package me.winds.wrapper.core.network;

import android.support.v4.util.ArrayMap;

import com.lzy.okgo.request.base.Request;

import java.io.Serializable;

import me.winds.wrapper.ManagerConfig;
import me.winds.wrapper.core.model.BaseVo;


/**
 * 请求体
 */
public class RequestInfo {

    public static final int REQUEST_GET = 100; //查询数据请求
    public static final int REQUEST_POST = 200; //新增和修改数据请求
    public static final int REQUEST_POST_JSON = 201; //post 上传json
    public static final int REQUEST_PUT = 300; //put请求
    public static final int REQUEST_PUT_JSON = 301; //put请求 直接上传json
    public static final int REQUEST_CUSTOM = 400; //自定义请求
    public static final int REQUEST_DELETE = 500; //删除请求

    public final String host = ManagerConfig.getInstance().getBaseHost(); //请求默认的domain

    public String url;  //请求完整的url
    public String api; //请求的api
    public Request request;
    public Class<? extends BaseVo> dataClass; //请求结果Vo的class

    public int requestType; //请求的类型
    public int actionType; //请求接口要做的操作类型
    public long dataExpireTime; //数据缓存时间默认为0即不缓存数据 只对get请求生效

    public ArrayMap<String, Serializable> requestParams; //请求参数
    public ArrayMap<String, String> headerParams; //请求头参数

    public boolean needShowDialog = true; //是否需要显示加载对话框
    public boolean needMockData = false; //是否需要模拟数据，此标识仅用于接口已定义，但未实现的前提下才能设置为true


    public RequestInfo(String api, Class<? extends BaseVo> dataClass) {
        this.api = api;
        this.dataClass = dataClass == null ? BaseVo.class : dataClass;
        this.url = String.format("%1$s%2$s", host, api);
    }

    public RequestInfo(String url, long dataExpireTime, Class<? extends BaseVo> dataClass) {
        this(url, dataClass);
        this.dataExpireTime = dataExpireTime;
    }

    public RequestInfo(boolean isDirect, String api, Class<? extends BaseVo> dataClass) {
        this.api = api;
        this.dataClass = dataClass == null ? BaseVo.class : dataClass;
        this.url = isDirect ? api : String.format("%1$s%2$s", host, api);
    }

    public RequestInfo(boolean isDirect, String api, Class<? extends BaseVo> dataClass, ArrayMap<String, String> headerParams) {
        this.api = api;
        this.dataClass = dataClass == null ? BaseVo.class : dataClass;
        this.url = isDirect ? api : String.format("%1$s%2$s", host, api);
        this.headerParams = headerParams;
    }


    /**
     * @param isDirect
     * @param requestType  此处用于扩展  此处需要主动在请求体内做处理
     * @param api
     * @param dataClass
     * @param headerParams
     */
    public RequestInfo(boolean isDirect, int requestType, String api, Class<? extends BaseVo> dataClass, ArrayMap<String, String> headerParams) {
        this.api = api;
        this.requestType = requestType;
        this.dataClass = dataClass;
        this.url = isDirect ? api : String.format("%1$s%2$s", host, api);
        this.headerParams = headerParams;
    }

    /**
     * 用于自定义请求
     *
     * @param request
     * @param api
     */
    public RequestInfo(Request request, boolean isDirect, String api, Class<? extends BaseVo> dataClass) {
        this.api = api;
        this.url = isDirect ? api : String.format("%1$s%2$s", host, api);
        this.requestType = REQUEST_CUSTOM;
        this.request = request;
        this.dataClass = dataClass;
    }


    public Class<? extends BaseVo> getDataClass() {
        return dataClass;
    }

    public void setDataClass(Class<? extends BaseVo> dataClass) {
        this.dataClass = dataClass;
    }


    public int getActionType() {
        return actionType;
    }

    public void setActionType(int actionType) {
        this.actionType = actionType;
    }

    public String getApi() {
        return api;
    }

    public void setApi(String api) {
        this.api = api;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getRequestType() {
        return requestType;
    }

    public void setRequestType(int requestType) {
        this.requestType = requestType;
    }

    public long getDataExpireTime() {
        return dataExpireTime;
    }

    public void setDataExpireTime(long dataExpireTime) {
        this.dataExpireTime = dataExpireTime;
    }

    public ArrayMap<String, Serializable> getRequestParams() {
        return requestParams;
    }

    public ArrayMap<String, Serializable> put(String key, Serializable value) {
        requestParams.put(key, value);
        return requestParams;
    }

    public void setRequestParams(ArrayMap<String, Serializable> requestParams) {
        this.requestParams = requestParams;
    }

    public ArrayMap<String, String> getHeaderParams() {
        return headerParams;
    }

    public void setHeaderParams(ArrayMap<String, String> headerParams) {
        this.headerParams = headerParams;
    }

    public boolean isNeedShowDialog() {
        return needShowDialog;
    }

    public void setNeedShowDialog(boolean needShowDialog) {
        this.needShowDialog = needShowDialog;
    }

    public boolean isNeedMockData() {
        return needMockData;
    }

    public void setNeedMockData(boolean needMockData) {
        this.needMockData = needMockData;
    }
}
